package com.example.demo.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("deploy")
public class DeployController {
	
	@GetMapping("dev")
	@ResponseBody
	public String dev() throws IOException, InterruptedException {

        exec("docker pull registry.cn-shenzhen.aliyuncs.com/freshconnect/scmall-front-web");
    	exec("docker pull registry.cn-shenzhen.aliyuncs.com/freshconnect/scmall-admin-web");
    	exec("docker stop scmall-front-web");
        exec("docker rm scmall-front-web");
    	exec("docker run --name scmall-front-web -d -p 8888:8080 registry.cn-shenzhen.aliyuncs.com/freshconnect/scmall-front-web");
    	exec("docker stop scmall-admin-web");
        exec("docker rm scmall-admin-web");
    	exec("docker run --name scmall-admin-web -d -p 8889:8080 registry.cn-shenzhen.aliyuncs.com/freshconnect/scmall-admin-web");
    	//删除被废弃的镜像，防止磁盘满
    	exec("docker image rm $(docker images | grep 'none' | awk '{print $3}')");
		return "deploy success";
	}
	
	@GetMapping("test")
	@ResponseBody
	public String deploy() throws IOException, InterruptedException {

        exec("docker pull registry.cn-shenzhen.aliyuncs.com/freshconnect/scmall-front-web");
    	exec("docker pull registry.cn-shenzhen.aliyuncs.com/freshconnect/scmall-admin-web");
    	exec("docker stop scmall-front-web-test");
        exec("docker rm scmall-front-web-test");
    	exec("docker run --name scmall-front-web-test -d -p 8891:8080 registry.cn-shenzhen.aliyuncs.com/freshconnect/scmall-front-web");
    	exec("docker stop scmall-admin-web-test");
        exec("docker rm scmall-admin-web-test");
    	exec("docker run --name scmall-admin-web-test -d -p 8892:8080 registry.cn-shenzhen.aliyuncs.com/freshconnect/scmall-admin-web");
		
		return "deploy test success";
	}
	
	@GetMapping("prod")
	@ResponseBody
	public String prod() throws IOException, InterruptedException {

        exec("docker pull registry.cn-shenzhen.aliyuncs.com/freshconnect/scmall-front-web");
    	exec("docker pull registry.cn-shenzhen.aliyuncs.com/freshconnect/scmall-admin-web");
    	exec("docker stop scmall-front-web-prod");
        exec("docker rm scmall-front-web-prod");
    	exec("docker run --name scmall-front-web-prod -d -p 8893:8080 registry.cn-shenzhen.aliyuncs.com/freshconnect/scmall-front-web");
    	exec("docker stop scmall-admin-web-prod");
        exec("docker rm scmall-admin-web-prod");
    	exec("docker run --name scmall-admin-web-prod -d -p 8894:8080 registry.cn-shenzhen.aliyuncs.com/freshconnect/scmall-admin-web");
		
		return "deploy prod success";
	}
	
    public static String exec(String command) throws InterruptedException {
        String returnString = "";
        Process pro = null;
        Runtime runTime = Runtime.getRuntime();
        if (runTime == null) {
            System.err.println("Create runtime false!");
        }
        try {
            pro = runTime.exec(command);
            BufferedReader input = new BufferedReader(new InputStreamReader(pro.getInputStream()));
            PrintWriter output = new PrintWriter(new OutputStreamWriter(pro.getOutputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                returnString = returnString + line + "\n";
                System.out.println(returnString);
            }
            input.close();
            output.close();
//            pro.destroy();
        } catch (IOException ex) {
            Logger.getLogger(DeployController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return returnString;
    }

}
